package com.cts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cts.entity.LoginLog;
import com.cts.entity.OperationLog;
import com.cts.mapper.LoginLogMapper;
import com.cts.model.dto.LogDTO;
import com.cts.model.vo.PageResultVO;
import com.cts.service.LoginLogService;
import com.cts.util.DateFormatUtils;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
* @author tongshengchen
* @description 针对表【login_log】的数据库操作Service实现
* @createDate 2023-02-06 12:25:57
*/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
    implements LoginLogService {

    @Autowired
    LoginLogMapper loginLogMapper;

    @Autowired
    DateFormatUtils dateFormatUtils;

    @Override
    public PageResultVO getLoginLog(LogDTO logDTO) {
        LambdaQueryWrapper<LoginLog> loginLogLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(!StringUtils.isNullOrEmpty(logDTO.getBeginTime()) && !StringUtils.isNullOrEmpty((logDTO.getEndTime()))){
            String beginTime = logDTO.getBeginTime();
            String endTime = logDTO.getEndTime();

            if(beginTime.equals(endTime)) {
                LocalDate formatEndTime = dateFormatUtils.StringToLocalDate(logDTO.getEndTime(), "yyyy-MM-dd");
                endTime = dateFormatUtils.LocalDateToString(formatEndTime.plusDays(1), "yyyy-MM-dd");
            }
            loginLogLambdaQueryWrapper.between(LoginLog::getCreateTime, beginTime, endTime);
        }

        Page<LoginLog> loginLogPage = new Page<>(logDTO.getPageNo(), logDTO.getPageSize());
        Page<LoginLog> loginLogPageRes = loginLogMapper.selectPage(loginLogPage,loginLogLambdaQueryWrapper);
        return new PageResultVO(loginLogPageRes.getTotal(), loginLogPageRes.getRecords());
    }
}




